package com.lineage.server.timecontroller.event;

import com.lineage.config.ConfigDescs;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.thread.GeneralThreadPool;
import com.lineage.server.world.World;
import java.util.TimerTask;
import java.util.concurrent.ScheduledFuture;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class NewServerTime extends TimerTask {
  private static final Log _log = LogFactory.getLog(NewServerTime.class);
  
  private int _count = 1;
  
  private int _time = 0;
  
  private ScheduledFuture<?> _timer;
  
  public void start(int time) {
    this._time = time;
    int timeMillis = time * 1000;
    this._timer = GeneralThreadPool.get().scheduleAtFixedRate(this, timeMillis, timeMillis);
  }
  
  public void run() {
    try {
      World.get().broadcastPacketToAll((ServerBasePacket)new S_SystemMessage(ConfigDescs.getShow(this._count)));
      this._count++;
      if (this._count >= ConfigDescs.get_show_size())
        this._count = 1; 
    } catch (Exception e) {
      _log.error("服务器介绍与教学时间轴异常重启", e);
      GeneralThreadPool.get().cancel(this._timer, false);
      NewServerTime timerTask = new NewServerTime();
      timerTask.start(this._time);
    } 
  }
}
